# Database
import io
import asyncpg
from sqlalchemy import (create_engine,inspect,
                        Engine, MetaData,Table,Column,
                        String,Date,Float,BigInteger)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import sessionmaker

from models.model import Base

import pandas as pd

PG_CONF={
    "user":"hg101",
    "password":"124578963hzm",
    "host":"localhost",
    "port":"5432",
    "database":"hg_quant",
}

DATABASE_URL = f"postgresql://{PG_CONF['user']}:{PG_CONF['password']}\
@{PG_CONF['host']}:{PG_CONF['port']}/{PG_CONF['database']}"

engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


def save_by_copy(df: pd.DataFrame,table_name:str,engine:Engine ):
    pass

def check_model_table():
    model_tables = [table for table in Base.metadata.tables]
    print("Current Model Tables :")
    print(model_tables)
    return model_tables

def check_db_tables():
    inspector = inspect(engine)
    exsiting_tables = [table for table in inspector.get_table_names()]
    print("Current Database Tables :")
    print(exsiting_tables)
    return exsiting_tables

def reset_all_table():
    print("正在删除所有表...")
    Base.metadata.drop_all(bind=engine)

    Base.metadata.create_all(bind=engine)
    print("数据库表已重置完成")

def create_all_table():
    Base.metadata.create_all(bind=engine)
    print("表已创建（如果不存在）")

# def drop_all_table():

if __name__ == "__main__":
    import sys
    if len(sys.argv)>1 and sys.argv[1] == "reset":
        reset_all_table()
    elif len(sys.argv)>1 and sys.argv[1] == "create":
        create_all_table()
    elif len(sys.argv)>1 and sys.argv[1] == "check_model_tables":
        check_model_table()
    elif len(sys.argv)>1 and sys.argv[1] == "check_db_tables":
        check_db_tables()
    else:
        print("args: [reset,create]")